package com.fBlog.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fBlog.pojo.User;
import com.fBlog.service.impl.PhotoServiceI;
import com.fBlog.service.impl.SpeakServiceI;

@Controller
@RequestMapping("/speakController")
public class SpeakController {
	@Autowired
	private SpeakServiceI speakService;

	@RequestMapping(value = "/upload", produces = "application/json;charset=UTF-8")
	public @ResponseBody void uploadFiles(HttpServletRequest request, HttpServletResponse response,@RequestParam("picture") MultipartFile [] files) {
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//获取session中的用户信息
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String userId = user.getId();
		String content = request.getParameter("talk");
		String path = request.getServletContext().getRealPath(File.separator+"images"+File.separator+"speakPicture");

		//保证每个相册都存在相应的文件夹
		File f = new File(path);
		if(!f.exists()){
			f.mkdirs();
		}
		String realPath = "";
		String uniqueName = "";
		if (!files[0].isEmpty()) { 
			uniqueName=files[0].getOriginalFilename();//得到文件名
			//获取图片后缀，限制只能上传图片格式：jpg，png等，目前只允许上传这两种，以后可以再加格式。
			String img = uniqueName.substring(uniqueName.lastIndexOf(".") + 1).trim().toLowerCase();
			if(img.equals("jpg")||img.equals("png")||img.equals("gif")){
				realPath=path+File.separator+uniqueName;//说说图片保存
				try {
					files[0].transferTo(new File(realPath));
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}   // 转存文件
			}else{
				//图片格式错误标记"1"
				writer.print("1");  
			}
		}
		//保存该条动态信息
		String code = speakService.addDynamics(uniqueName, userId,content);
		//上传成功标记"0"
		writer.print(code);
	}
	
	@RequestMapping("/comment")
	public void comment(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="speakId") String speakId, @RequestParam(value="comment") String comment){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String userName = user.getUserName();
		speakService.addComment(userName,speakId,comment);
	}
	@RequestMapping("/selfComment")
	public void selfComment(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="commentId") String commentId, @RequestParam(value="content") String content){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String userName = user.getUserName();
		speakService.addSelfComment(userName,commentId,content);
	}
	@RequestMapping("/good")
	public void good(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="speakId") String speakId){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		String userName = user.getUserName();
		speakService.addGood(userName,speakId);
	}
	@RequestMapping("/deleteCom")
	public void deleteCom(HttpServletRequest request, HttpServletResponse response,@RequestParam(value="commentId") String commentId){
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(speakService.deleteCom(commentId)){
			//删除成功，返回true
			writer.print("0");
		}
	}
	@RequestMapping("/deleteSpeak")
	public void deleteSpeak(HttpServletRequest request, HttpServletResponse response,@RequestParam(value="speakId") String speakId){
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(speakService.deleteSpeak(speakId)){
			//删除成功，返回true
			writer.print("0");
		}
	}
}
